package admin

import (
	"encoding/json"
	"ginshop02/models"
	"github.com/gin-contrib/sessions"
	"net/http"

	"github.com/gin-gonic/gin"
)

type MainController struct{}

func (con MainController) Index(c *gin.Context) {
	session := sessions.Default(c)
	userinfo := session.Get("userinfo")
	userinfoJson, ok := userinfo.([]byte)
	if ok {
		var userinfoStruct []models.Manager
		err := json.Unmarshal(userinfoJson, &userinfoStruct)
		if err != nil {
			c.Redirect(http.StatusFound, "/admin/login")
		}
		c.HTML(http.StatusOK, "admin/main/index.html", gin.H{
			"username": userinfoStruct[0].Username,
		})

	} else {
		c.Redirect(http.StatusFound, "/admin/login")
	}

}

func (con MainController) Welcome(c *gin.Context) {
	c.HTML(http.StatusOK, "admin/main/welcome.html", gin.H{})
}
